<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- class类封装promise -->
    <script>
        class MyPromise {
            constructor(fn) {
                // 尝试执行接收的函数 如果没有函数则抛出错误
                this.state = 'pending' // 表示进行中
                this.value = ''
                this.failed = [] // 这个数组用来收集promise执行失败时的函数
                this.success = [] // 这个数组用来收集promise执行成功的函数
                let resolve = (res) => {
                    if (this.state === 'pending') {
                        this.state = 'fullfilled' // 表示promise执行成功
                        this.value = res
                        this.success.forEach(item => {
                            item(res)
                        })
                    }
                }
                let reject = (err) => {
                    if (this.state === 'pending') {
                        this.state = 'rejected'
                        this.value = err
                        this.failed.forEach(item => {
                            item(err)
                        })
                    }
                }
                try {
                    fn(resolve, reject)
                } catch (err) {
                    throw new Error(err)
                }
            }

            then(success, failed) {
                this.success.push(success)
                this.failed.push(failed)
            }
        }


        let promise = new MyPromise(function(resolve, reject) {
            // resolve(1)
            setTimeout(() => {
                // reject('promise执行失败了')
                resolve('这是异步操作执行成功后的数据')
            }, 3000);

        });
        // console.log(promise);
        promise.then(res => {
            console.log(res);
        }, err => {
            console.log(err);
        });
        // const pn = Promise.reject('出错了');
        // // 等同于
        // const pen = new Promise((resolve, reject) => reject('出错了呀'))

        // pen.then(null, function(s) {
        //     console.log(s)
        // });

        // Promise.reject('出错了')
        //     .catch(e => {
        //         console.log(e === '出错了')
        //     });
        // true
        function getFoo() {
            return new Promise(function(resolve, reject) {
                resolve('你是个傻得');
            });
        }

        const g = function*() {
            try {
                const foo = yield getFoo();
                console.log(foo);
            } catch (e) {
                console.log(e);
            }
        };

        function run(generator) {
            const it = generator();

            function go(result) {
                if (result.done) return result.value;

                return result.value.then(function(value) {
                    return go(it.next(value));
                }, function(error) {
                    return go(it.throw(error));
                });
            }

            go(it.next());
        }

        run(g);
    </script>

</body>

</html>